package com.channel.temperature.collect.app.bleupdate;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.channel.temperature.collect.R;

import java.util.ArrayList;
import java.util.List;


public class BinAdapter extends BaseAdapter {

    private Context mContext;

    private List<BinBean> mDataList;

    public BinAdapter(Context context) {
        mContext = context;
        mDataList = new ArrayList<BinBean>();
    }

    public void setDataList(List<BinBean> datas) {
        mDataList.clear();
        mDataList.addAll(datas);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public Object getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    private static class ViewHolder {
        TextView rb;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_bin, null, false);

            holder = new ViewHolder();
            holder.rb = convertView.findViewById(R.id.item_rb);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final BinBean result = (BinBean) getItem(position);
        holder.rb.setText(result.getName());
        if (result.isChecked()) {
            holder.rb.setBackgroundColor(mContext.getResources().getColor(R.color.colorAccent));
            holder.rb.setTextColor(mContext.getResources().getColor(R.color.white));
        } else {
            holder.rb.setBackgroundColor(mContext.getResources().getColor(R.color.white));
            holder.rb.setTextColor(mContext.getResources().getColor(R.color.black));
        }
        return convertView;
    }

}
